package com.yuwubao.hz_International.controllers.app

import com.alipay.api.DefaultAlipayClient
import com.alipay.api.domain.AlipayTradeAppPayModel
import com.alipay.api.internal.util.AlipaySignature
import com.alipay.api.request.AlipayTradeAppPayRequest
import com.yuwubao.hz_International.annotation.NeedUser
import com.yuwubao.hz_International.entities.StoreEntity
import com.yuwubao.hz_International.entities.vo.ConsumptionVo
import com.yuwubao.hz_International.entities.vo.MyCoin
import com.yuwubao.hz_International.entities.vo.MyCoins
import com.yuwubao.hz_International.entities.vo.MyCollectionVo
import com.yuwubao.hz_International.services.AppService
import com.yuwubao.hz_International.util.AlipayConfig
import com.yuwubao.hz_International.util.Const
import com.yuwubao.hz_International.util.RestApiResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.slf4j.LoggerFactory
import org.springframework.data.domain.Page
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@RestController
@RequestMapping("app/index")
@CrossOrigin
@Api(value = "app接口文档", description = "app接口")
open class AppApi(val appService: AppService) {
    private val Logger = LoggerFactory.getLogger(AppApi::class.java)

    //    @Resource lateinit var appService:AppService
//    @Autowired private lateinit var appService
//    支付宝充值 被推荐人可得到礼券
    @PostMapping("recharge")
    @ApiOperation("充值接口 未调用支付宝")
    @NeedUser
    fun recharge(@RequestParam userId: Int, @RequestParam money: Double): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        appService.recharge(userId, money)
        result.successResponse(true)
        return result
    }

    @GetMapping("beforeRecharge")
    @ApiOperation("充值和兑换积分之前显示余额以及利率")
    @NeedUser
    fun beforeRecharge(@RequestParam userId: Int, @ApiParam(required = false, value = "类型 0 充值 1兑换") @RequestParam(required = false, defaultValue = "0") type: Int): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        val myCoin: MyCoin = appService.beforeRecharge(userId, type)
        result.successResponse(myCoin)
        return result
    }

    @PostMapping("toRedeem")
    @ApiOperation("兑换积分")
    @NeedUser
    fun toRedeem(@ApiParam(required = true, value = "用户id") @RequestParam userId: Int,
                 @ApiParam(required = true, value = "兑换的消费金数量") @RequestParam money: Double): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        appService.toRedeem(userId, money)
        result.successResponse(true)
        return result

    }

    @PostMapping("top_up_callback")
    fun demo(req: HttpServletRequest, res: HttpServletResponse): Boolean {
        val maps = hashMapOf<String, String>()
        val requestParams = req.parameterMap
        val iter = requestParams.keys.iterator()
        while (iter.hasNext()) {
            val name = iter.next() as String
            val values = requestParams[name] as Array<String>
            var valueStr = ""
            for (i in values.indices) {
                valueStr = if (i == values.size - 1)
                    valueStr + values[i]
                else
                    valueStr + values[i] + ","
            }
            maps[name] = valueStr
        }
        Logger.info("maps:$maps")
        val flag = AlipaySignature.rsaCheckV1(maps,AlipayConfig.CHECK_KEY,AlipayConfig.CHARSET,AlipayConfig.SIGNTYPE)
        Logger.info("flag:$flag")
        return flag
    }

    @GetMapping("demo1")
    fun demo1() {
        val alipayClient = DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.PRIVATE_KEY, AlipayConfig.FORMAT
                , AlipayConfig.CHARSET, AlipayConfig.PUBLIC_KEY, AlipayConfig.SIGNTYPE)
        val payRequest = AlipayTradeAppPayRequest()
        val model = AlipayTradeAppPayModel()
        model.outTradeNo = "123123132131"
        model.timeoutExpress = "90m"
        model.totalAmount = "1.00"
        model.productCode = "QUICK_MSECURITY_PAY"
        model.subject = "大乐透"
        payRequest.bizModel = model
        payRequest.notifyUrl = "http://www.allblux.xyz/app/index/top_up_callback"


        val res = alipayClient.sdkExecute(payRequest)
        Logger.info("body:${res.body}")
    }



    //首页banner图
    @GetMapping("banner")
    @ApiOperation(value = "首页banner图")
    fun banner(): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        val bannerPage = appService.getBanner()
        result.successResponse(bannerPage)
        return result
    }

    @GetMapping("store")
    @ApiOperation(value = "商家列表")
    fun getStore(@ApiParam(required = false, value = "第几页") @RequestParam(required = false, defaultValue = "1") index: Int,
                 @ApiParam(required = false, value = "每页几个") @RequestParam(required = false, defaultValue = "10") size: Int): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        val storePage: Page<StoreEntity>? = appService.getStore(index, size)
        result.successResponse(Const.SUCCESS, storePage)
        return result
    }

    @GetMapping("consumptionInfo")
    @ApiOperation(value = "各种金币明细")
    @NeedUser
    fun consumptionInfo(@ApiParam(required = true, value = "用户id") @RequestParam userId: Int, @ApiParam(required = true, value = "类型 0消费金 1福利金 2积分 3礼券 4收款明细") @RequestParam type: Int,
                        @ApiParam(required = false, value = "第几页") @RequestParam(required = false, defaultValue = "1") index: Int,
                        @ApiParam(required = false, value = "每页多少") @RequestParam(required = false, defaultValue = "10") size: Int): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        val consumptionVos: Page<ConsumptionVo>? = appService.consumptionVos(userId, index, size, type)
        result.successResponse(consumptionVos)
        return result
    }

    @GetMapping("myCoin")
    @ApiOperation("我的各种货币")
    @NeedUser
    fun myConsumption(@ApiParam(required = true, value = "用户id") @RequestParam userId: Int,
                      @ApiParam(required = false, value = "类型 0消费金 1积分 2福利金 3礼券 不传值为返回当前用户余额") @RequestParam(required = false, defaultValue = "-1") type: Int): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        if (type != -1) {
            val myCoin = appService.getMyCoin(userId, type)
            result.successResponse(myCoin)
        } else {
            val myCoins: MyCoins = appService.getMyCoins(userId)
            result.successResponse(myCoins)
        }
        return result
    }


    @GetMapping("myQrCode")
    @ApiOperation("我的收款码")
    @NeedUser
    fun myQrCode(@ApiParam(required = true, value = "用户id") @RequestParam userId: Int): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        val myQrCode = appService.findMyQrCode(userId)
        result.successResponse(myQrCode)
        return result
    }

    @PostMapping("toWelfare")
    @ApiOperation("兑换福利金")
    @NeedUser
    fun toWelfare(@RequestParam userId: Int, @ApiParam(required = true, value = "兑换的消费金数量") @RequestParam money: Double): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        appService.toWelfare(userId, money)
        result.successResponse(true)
        return result
    }

    @GetMapping("customer")
    @ApiOperation("客服")
    @NeedUser
    fun customer(): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        val str: String = appService.getCustomer()
        result.successResponse(str)
        return result
    }

    @GetMapping("myFans")
    @ApiOperation("我的粉丝")
    @NeedUser
    fun myFans(@ApiParam(required = true, value = "用户Id") @RequestParam userId: Int,
               @ApiParam(required = false, value = "第几页") @RequestParam(required = false, defaultValue = "1") index: Int,
               @ApiParam(required = false, value = "每页多少") @RequestParam(required = false, defaultValue = "10") size: Int): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        val entity = appService.getMyFans(userId, index, size)
        result.successResponse(entity)
        return result
    }

    @PostMapping("withdrawal")
    @ApiOperation("福利金提现")
    @NeedUser
    fun withdrawal(@ApiParam(required = true, value = "用户id") @RequestParam userId: Int,
                   @ApiParam(required = true, value = "提现的数量") @RequestParam money: Double): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        appService.withdrawal(userId, money)
        result.successResponse(true)
        return result
    }

    @GetMapping("beforeWithdrawal")
    @ApiOperation("福利金提现页面 ")
    @NeedUser
    fun beforeWithdrawal(@ApiParam(required = true, value = "用户id") @RequestParam userId: Int): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        val entity = appService.beforeWithdrawal(userId)
        result.successResponse(entity)
        return result
    }

    @PostMapping("pay")
    @ApiOperation("付款")
    @NeedUser
    fun pay(@ApiParam(required = true, value = "用户id") @RequestParam userId: Int,
            @ApiParam(required = true, value = "商户二维码信息") @RequestParam qrCode: Int,
            @ApiParam(required = true, value = "付款数") @RequestParam money: Double): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        appService.pay(userId, qrCode, money)
        result.successResponse(true)
        return result
    }

    @GetMapping("myCollection")
    @ApiOperation("收款明细")
    @NeedUser
    fun myCollection(@ApiParam(required = true, value = "用户id") @RequestParam userId: Int,
                     @ApiParam(required = false, value = "类型 默认0商户收款 1 用户付款") @RequestParam(required = false, defaultValue = "0") type: Int,
                     @ApiParam(required = false, value = "第几页") @RequestParam(required = false, defaultValue = "1") index: Int,
                     @ApiParam(required = false, value = "每页多少") @RequestParam(required = false, defaultValue = "10") size: Int): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        val myPages: Page<MyCollectionVo> = appService.getMyCollections(userId, type, index, size)
        result.successResponse(myPages)
        return result
    }

    @PostMapping("updatePassword")
    @ApiOperation("修改密码")
    @NeedUser
    fun updatePassword(@ApiParam(required = true, value = "手机号") @RequestParam phone: String,
                       @ApiParam(required = true, value = "密码") @RequestParam password: String): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        appService.updatePassword(phone, password)
        result.successResponse(true)
        return result
    }


}